<?php
if($_POST['submit'] == 'Submit'){
	$end_date = $_POST['p_end_date'];
	$getdata = "select * from tbl_period where p_end_date = '$end_date'";
	$result_count = mysql_num_rows(mysql_query($getdata));
	extract($_POST);
	if($split_trunk == 'split_trunk')
	{
		if($p_split == 'p_split_no_trunk'){echo $p_split_no_trunk = true;}
		else if($p_split == 'p_split_trunk'){echo $p_split_trunk = true;}
		
		//insert into tbl_tale_split
		if($result_count < 1){	
		if($p_split == 'p_split_no_trunk'){
			$notrunk_split = "INSERT INTO tbl_period_split (p_end_date_trunk,p_end_date_no_trunk) VALUES ('','$p_end_date')";
			mysql_query($notrunk_split);
		}else{
			$trunk_split = "INSERT INTO tbl_period_split (p_end_date_trunk,p_end_date_no_trunk) VALUES ('$p_end_date','')";
			mysql_query($trunk_split);
		}
		}				
	}
	//insert first-half billing period
	if($result_count < 1){
	$insert_query = "INSERT INTO tbl_period (p_start_date,p_end_date,p_split_no_trunk,p_split_trunk) VALUES ('$p_start_date','$p_end_date','$p_split_no_trunk','$p_split_trunk')";
	}else{?>
	<script>
	$(document).ready(function(){
		$("#error").text("Billing Period End Date Already Exists!").show();
		return false;
	});
	</script>
		<?}
	
	// insert second-half billing period
	if($split_trunk == 'split_trunk')
	{
		$p_split_no_trunk = false;
		$p_split_trunk = false;
		if($p_split2 == 'p_split_no_trunk2'){$p_split_no_trunk = true;}
		else if($p_split2 == 'p_split_trunk2'){$p_split_trunk = true;}
		
		if($result_count < 1){	
		$insertbill2_query = "INSERT INTO tbl_period (p_start_date,p_end_date,p_split_no_trunk,p_split_trunk) VALUES ('$p_start_date2','$p_end_date2','$p_split_no_trunk','$p_split_trunk')";
		mysql_query($insertbill2_query);
		
		//insert into tbl_tale_split	
		if($p_split2 == 'p_split_no_trunk2'){
			$notrunk_split = "INSERT INTO tbl_period_split (p_end_date_trunk,p_end_date_no_trunk) VALUES ('','$p_end_date2')";
			mysql_query($notrunk_split);
		}else{
			$trunk_split = "INSERT INTO tbl_period_split (p_end_date_trunk,p_end_date_no_trunk) VALUES ('$p_end_date2','')";
			mysql_query($trunk_split);
		}
		}else{?>
			<script>
			$(document).ready(function(){
				$("#error").text("Billing Period End Date Already Exists!").show();
				return false;
			});
			</script>
			<?}
	}
	
	if(mysql_query($insert_query))
	{?>
	<script>
	$(document).ready(function(){
		$("#success").text("Billing Period Inserted Successfully!").show();
		return false;
	});
	</script>	
	<?}
}
?>
<script>
$(document).ready(function(){
//showup the checked split billing
$(".split_trunk").click(function () {
if ($('.split_trunk:checked').val() !== undefined) {
$(".split").slideDown('fast');
} else {
$(".split").slideUp('fast');
}
}); 
});
</script>

<script>
	$(function() {
		var dates = $( "#from2, #to2" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from2" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
	});
</script>

<span id='error'></span><span id='success'></span>
<fieldset style="font-size: 11px;color:#000000;">
<legend><strong><h3>Generate Billing Period</h3></strong></legend>
<form action="" method="post">
	<div class="billing_period">
		<div style="padding:5px;">
			<span>Start Date: </span>
			<span><input type="text" name="p_start_date" id="from" class="required p_start_date" /></span>
		</div>
		<div style="padding:5px;">
			<span style="padding-right:9px;">End Date: </span>
			<span><input type="text" name="p_end_date" id="to" class="required p_end_date" /></span>
		</div>
		<div style="padding:5px;">
			<span><strong>Split Billing Period</strong></span>
			<span><input type="checkbox" class="split_trunk" name="split_trunk" value="split_trunk" /></span>
		</div>
		<div style="display:none;" class="split">
			<div style="padding:5px;">
				<span>p_split_no_trunk </span>
				<span><input type="radio" class="p_split" name="p_split" value="p_split_no_trunk"/></span>
			</div>
			<div style="padding:5px;">
				<span style="padding-right:18px;">p_split_trunk </span>
				<span><input type="radio" class="p_split" name="p_split" value="p_split_trunk" /></span>
			</div>
			<br/><span><strong>Associated Billing Period</strong></span>
			<div style="padding:5px;">
				<span>Start Date: </span>
				<span><input type="text" name="p_start_date2" id="from2" class="required p_start_date2" /></span>
			</div>
			<div style="padding:5px;">
				<span style="padding-right:9px;">End Date: </span>
				<span><input type="text" name="p_end_date2" id="to2" class="required p_end_date2" /></span>
			</div>
			<div style="padding:5px;">
				<span>p_split_no_trunk </span>
				<span><input type="radio" class="p_split2" name="p_split2" value="p_split_no_trunk2"/></span>
			</div>
			<div style="padding:5px;">
				<span style="padding-right:18px;">p_split_trunk </span>
				<span><input type="radio" class="p_split2" name="p_split2" value="p_split_trunk2" /></span>
			</div>
		</div>
	<input type="submit" value="Submit" name="submit" />
	<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=report-mang'"/>
	</div>
</form>
</fieldset>